﻿@model ProductItem
<div>
    @using (Html.BeginForm())
    {
        <div class="modal-body">
            <p>
                <strong>@(Model.Name)</strong></p>
            <dl class="dl-horizontal">
                <dt>商品价格：</dt>
                <dd>
                    <input id="price" name="unitprice" type="text" value='@(Model.Price.ToString("N"))' class="input-mini" />
                </dd>
                @foreach (var o in Model.Skus)
                {
                    <dt>
                        @foreach (var s in o.SkuProperties)
                        {
                            <span>@(s.VName)</span>
                        }
                    </dt>
                    <dd>
                        <input name="id" type="hidden" value='@(o.Id)' />
                        <input name="price" type="text" maxlength="10" value='@(o.Price.ToString("N"))' class="input-mini" onkeyup="checkValue(this);"/>
                    </dd>
                }
            </dl>
        </div>
        <div class="modal-footer">
            <p id="message" class="pull-left color-red">
            </p>
            <a class="btn" href="#" data-dismiss="modal">取 消</a>
            <button type="button" class="btn btn-primary" onclick="submitForm(this.form,@(Model.Id))">
                确 认</button>
        </div>
    }
</div>
<script type="text/javascript">
    function checkPrice() {
        var array = [];
        var price = parseInt($('#price').val());
        var prices = $('input[name=quantity]');
        $.each(prices, function (i, o) {
            var price = parseInt($(o).val());
            array.push(price);
        });
        var min = _.min(array);
        var max = _.max(array);
        return (min <= price && price <= max);
    }
    function checkValue(obj) {
        var val = $(obj).val();
        if (!isNaN(val)) {
        } else {
            $(obj).val('');
        }
    }
    function submitForm(form, target) {
        if (!checkPrice) {
            $('#message', form).css('color', 'red').html('商品价格请设置在合理区间内！');
            return;
        }
        var param = $(form).serialize();
        $.postJSON(form.action, param, function (result) {
            if (result.Status) {
                $("#item-price-" + target).html(result.Data);
                $('#message', form).css('color', 'green').html("更新价格成功！");
            } else {
                $('#message', form).css('color', 'red').html(result.Message);
            }
        });
    }
</script>
